package com.example.scrambledpuzzle;




import android.net.Uri;
import android.os.Bundle;

import android.provider.MediaStore.MediaColumns;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.database.Cursor;

import android.view.Menu;
import android.view.View;

import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ProgressBar;

public class PhoneGalllery extends Activity   {
	
	private static int PICTURE_GALLERY = 1;
	private ProgressDialog progress;
	
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_phone_galllery);
		
	    Button buttonLoadImage = (Button) findViewById(R.id.buttonLoadPicture);
	    
	    
        buttonLoadImage.setOnClickListener(new View.OnClickListener() {
             
            @Override
            public void onClick(View arg0) {
            	
            	
                 
                Intent i = new Intent(
                        Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                 
                startActivityForResult(i, PICTURE_GALLERY);
                //finish();
            }
        });
        
        Button btnPreloadedImage=(Button) findViewById(R.id.buttonPreloadedPicture);
        btnPreloadedImage.setOnClickListener(new View.OnClickListener(){

			@Override
			public void onClick(View v) {
				Intent in= new Intent(PhoneGalllery.this,PreloadedImageGallery.class);
				
				startActivity(in);
				//finish();
			}
        	
        });

	}
	
	protected void onDestroy(){
		super.onDestroy();
	}
	
	
	@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        
       // final Bitmap the_image;
        super.onActivityResult(requestCode, resultCode, intent);
        
         if(requestCode == PICTURE_GALLERY){
        //switch (requestCode) {
		//case PICTURE_GALLERY:
		if (resultCode == RESULT_OK && intent != null) {
			Uri selectedImage = intent.getData();
			final String[] filePathColumn = { MediaColumns.DATA, MediaColumns.DISPLAY_NAME };
			Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
			// some devices (OS versions return an URI of com.android instead of com.google.android
			if (selectedImage.toString().startsWith("content://com.android.gallery3d.provider"))  {
				// use the com.google provider, not the com.android provider.
				selectedImage = Uri.parse(selectedImage.toString().replace("com.android.gallery3d","com.google.android.gallery3d"));
			}
			if (cursor != null) {
				cursor.moveToFirst();
				int columnIndex = cursor.getColumnIndex(MediaColumns.DATA);
				// if it is a picasa image on newer devices with OS 3.0 and up
				if (selectedImage.toString().startsWith("content://com.google.android.gallery3d")){
					columnIndex = cursor.getColumnIndex(MediaColumns.DISPLAY_NAME);
					if (columnIndex != -1) {
						//progress=new ProgressDialog(this);
						
						//progress_bar.setVisibility(View.VISIBLE);
						 Uri uriurl = selectedImage;
						Intent nextActivity=new Intent(this,SwapPuzzle.class);
						nextActivity.putExtra("GalleryPath", uriurl);
						startActivity(nextActivity);
						//finish();
					}
				} else { // it is a regular local image file
					String filePath = cursor.getString(columnIndex);
					cursor.close();
					//Bitmap the_image = BitmapFactory.decodeFile(filePath);
					//imageView.setImageBitmap(the_image);
					Intent i= new Intent(this,SwapPuzzle.class);
					
					i.putExtra("CameraPath", filePath);
					startActivity(i);
					//finish();
				}
			}
			// If it is a picasa image on devices running OS prior to 3.0
			else if (selectedImage != null && selectedImage.toString().length() > 0) {
				//progress_bar.setVisibility(View.VISIBLE);
				 
					Uri uriurl = selectedImage;
					Intent nextActivity=new Intent(this,SwapPuzzle.class);
					nextActivity.putExtra("GalleryPath", uriurl);
					startActivity(nextActivity);
					//finish();
				// Do this in a background thread, since we are fetching a large image from the web
				/*new Thread(new Runnable() {
					public void run() {
						final Bitmap the_image = getBitmap("image_file_name.jpg", uriurl);
						runOnUiThread(new Runnable(){

							@Override
							public void run() {
								imageView.setImageBitmap(the_image);
								
							}
							 
						 });
						
					}
				}).start();*/
			}
		}
		
	}
            
     
    }
	
	
	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.phone_galllery, menu);
		return true;
	}

}
